const websocket =require("ws")

const websocketServer=new websocket.Server({
port:8000
})

let clients={}
let clientsNum=0
//
ws.on("connection",(client)=>{
client.name=++clientNum
clients[client.name]=client

//
client.on("message",(msg)=>{
    console.log("用户"+client.name+"说："+msg);
broadcast(client,msg)

})
//监听报错
client.on("error",(err)=>{
    if(err){
        console,log(err);
    }
})
//监听下线
client.on("close",()=>{
    broadcastQuite(client)
    console.log
})

})

function broadcast(msg){
for(let key in clients){
    clients[key].send("用户"+client.name+"说"+msg);

}

}


//下线
function broadcastQuite(client){
    for(let key in clients){
        clients[key].send("系统提示：用户"+client.name+"下线了");
    
    }
    
    }
    

